<template>
    <!-- 球形波浪 -->
    <view v-if="type=='sphere'" :class="setSphereType" :style="setWidthHeight">
    	<view class="vus-progress-sphere" :style="setWidthHeight">
    		<view class="vus-progress-sphere-inner" :style="{'background-color':color[0]}">
                <!-- 水波纹 -->
                <view class="vus-progress-sphere-water" :class="assignAnim" :style="setSphereWater"></view>
                <!-- 高光 -->
                <view class="vus-progress-sphere-glare"></view>
                <!-- 文本 -->
                <view class="vus-progress-sphere-percent">
                	<text v-if="text" class="vus-progress-sphere-percent-text" :style="setText?text:''">{{assignPercent}}</text>
                    <text v-if="sphere.icon.length" :class="sphere.icon[0]" :style="sphere.icon&&sphere.icon[1]"></text>
                    <slot name="text">{{label}}</slot>
                </view>
                <!-- 气泡 -->
                <view v-if="sphere.bubble" v-for="item in sphereBubble" :key="item" class="vus-progress-sphere-bubble" :class="'bubble'+item"></view>
    		</view>
    	</view>
    </view>
</template>

<script>
export default {
    name: 'BlockProgress',
    props: {
        //进度条类型，球形波浪:sphere,
        type: {
            type: String,
            default: 'sphere',
            validator: val => ['sphere'].indexOf(val) > -1
        },
        // 球形波浪进度专用属性，方法：sphere={bubble:false,icon:['iconfont','style']}
        sphere: {
            type: Object,
            default() {
                return {
                    icon: [],
                    bubble: true
                }
            }
        },
        // 进度百份比
        percent: {
            type: [String, Number],
            default: 0
        },
        // 进度条动画效果 true/false
        anim: {
            type: Boolean,
            default: false
        },
        // 宽度
        width: {
            type: Number,
            default: 300
        },
        // 文本(支持两种写法)
        // text="font-size:20rpx;color:#f00;"
        // :text="true"
        text: {
            type: [Boolean, String],
            default: true
        },
		// 分别为：['背景','底部','顶部'] 
        color: {
            type: Array,
            default: () => {
                return ['#6C250F','#F4BD7D','#CC6B33']// ['#333','#c00','#f80']
            }
        },
		label:{
			type: [String],
			default: 'zkb'
		}
    },
    data() {
        return {
			sysInfo: uni.getSystemInfoSync(),
            sphereBubble: [1,2,3,4,5],
			assignAnim: '',
            assignPercent: this.percent,
        };
    },
    computed: {
        setSphereType() {
            let percent = this.assignPercent;
            let colorType = 200000 / 3;
            if (percent && !isNaN(percent) && percent >= 0) {
                if (percent < colorType * 1) {
                    return 'vus-progress-sphere-red';
                } else  if (percent < colorType * 2) {
                    return 'vus-progress-sphere-orange';
                } else {
                    return 'vus-progress-sphere-green';
                }
            }
        },
        setSphereWater() {
            let percent = this.assignPercent;
            percent = 200000 - percent;
            let styles = '';
			// 自定义颜色
            if (this.color.length == 3) {
                styles = `background-image:radial-gradient(${this.color[1]} 15%, ${this.color[2]});`;
            } else if (this.color.length == 2) {
                styles = `background-image:none;background-color:${this.color[1]};`;
            }
            return `top:${percent/2000}%;${styles}`;
        },
        setWidthHeight() {
            if (this.width) {
                // 球形水波动画宽高
                if (this.type == 'sphere') {
                    return `width:${this.width}rpx;height:${this.width}rpx;`
                }
            }
        },
        // 文本类型
        setText() {
            return typeof this.text === 'string';
        }
    },
    created() {
		if (this.type == 'sphere') {
            // 球形水波如果加动画，在IOS下会反复闪现
			if (this.sysInfo.platform !== 'ios' && this.anim) {
                this.assignAnim = 'vus-progress-sphere-anim';
			}
		}
    },
    watch: {
		percent(newVal) {
            this.assignPercent = newVal;
            // 完成时清除进度动画
            if (newVal >= 200000) {
                this.assignAnim = '';
            }
        }
	}
};
</script>

<style scoped lang="less" src="./BlockProgress.less"></style>
